package com.itheima.task;

import com.itheima.common.constant.RedisConstant;
import com.itheima.config.AliOssProperties;
import com.itheima.mapper.SetmealMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class ImgDeleteTask {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private SetmealMapper setmealMapper;
    @Scheduled(cron = "0 0 0 * * ?") //每日0点执行
    public void deleteImg() {
        log.info("定时删除多余图片");
        List<String> keys = setmealMapper.getAllImgFilePath();
        List<String> allObjects = aliOssProperties.getAllObjects();

        for (String object : allObjects) {
            boolean found = false;
            for (String key : keys) {
                if (key.contains(object)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                aliOssProperties.deleteObject(object);
                log.info("删除图片：{}", object);
            }
        }

        log.info("定时删除多余图片完成");
    }
}
